package com.timesheet.gui.tasktracker.controller;

import java.util.Date;

import com.example.timesheet.uitl.DateUtil;
import com.example.timesheet.uitl.TaskUtils;
import com.timesheet.gui.tasktracker.composites.TaskTrackerHistotyComposite;
import com.timesheet.server.common.models.TaskTrackerDto;
import com.timesheet.serverice.TaskTrackerService;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.beans.PojoObservables;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.jface.databinding.viewers.ViewersObservables;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;

public class TaskTrackerHistotyCompositeController {

	TaskTrackerHistotyComposite taskTrackerHistotyComposite;

	DataBindingContext bindingContext = new DataBindingContext();

	TaskTrackerDto dto = new TaskTrackerDto();

	public TaskTrackerHistotyCompositeController(TaskTrackerHistotyComposite taskTrackerHistotyComposite) {
		this.taskTrackerHistotyComposite = taskTrackerHistotyComposite;

		setDefaultValues();
		initDataBindings();
		performActions();

	}

	private void setDefaultValues() {
		taskTrackerHistotyComposite.getComboTaskTypeViewer().setContentProvider(ArrayContentProvider.getInstance());

		taskTrackerHistotyComposite.getComboTaskTypeViewer().setInput(getTaskTypes());

		taskTrackerHistotyComposite.getCmbTaskStatusViewer().setContentProvider(ArrayContentProvider.getInstance());
		taskTrackerHistotyComposite.getCmbTaskStatusViewer().setInput(TaskUtils.getTaskStatus());

		dto.setProjectId("ABSLIFE");
		dto.setEmployeeId("cb4yf71");

	}

	public String[] getTaskTypes() {
		return TaskUtils.getTaskTypes();
	}

	private void performActions() {
		taskTrackerHistotyComposite.getBtnSave().addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {

				int startDay = taskTrackerHistotyComposite.getDtStartDate().getDay();
				int startMonth = taskTrackerHistotyComposite.getDtStartDate().getMonth();
				int startYear = taskTrackerHistotyComposite.getDtStartDate().getYear();

				int endDay = taskTrackerHistotyComposite.getDtEndDate().getDay();
				int endMonth = taskTrackerHistotyComposite.getDtEndDate().getMonth();
				int endYear = taskTrackerHistotyComposite.getDtEndDate().getYear();

				Date startDate = DateUtil.createDate(startDay, startMonth, startYear);
				Date endDate = DateUtil.createDate(endDay, endMonth, endYear);

				dto.setStartDate(startDate);
				dto.setEndDate(endDate);

				boolean isCreated = new TaskTrackerService().createTaskTracker(dto);
				Shell shell = taskTrackerHistotyComposite.getShell();
				if (isCreated) {
					MessageDialog.openInformation(shell, "Info", "Info for you");
				}
				else {
					MessageDialog.openError(shell, "Error", "Error occured");
				}

				System.out.println("Hello: " + isCreated);
				System.out.println(dto.getTaskType());
			}
		});

	}

	private void initDataBindings() {

		// bind(taskTrackerHistotyComposite.getDtEndDate(), "endDate");
		// bind(taskTrackerHistotyComposite.getDtStartDate(), "startDate");
		bind(taskTrackerHistotyComposite.getTxtActualEffort(), "actualEffort");
		bind(taskTrackerHistotyComposite.getTxtDescription(), "description");
		bind(taskTrackerHistotyComposite.getTxtPlannedEffort(), "plannedEffort");
		bind(taskTrackerHistotyComposite.getTxtReason(), "reason");
		bind(taskTrackerHistotyComposite.getTxtTaskNo(), "taskNo");
		bind(taskTrackerHistotyComposite.getTxtTaskName(), "taskName");
		bind(taskTrackerHistotyComposite.getCmbTaskStatusViewer(), "taskStatus");
		bind(taskTrackerHistotyComposite.getComboTaskTypeViewer(), "taskType");
		bind(taskTrackerHistotyComposite.getTxtRelease(), "release");
		//bind(taskTrackerHistotyComposite.getTxtFakeEffort(), "fakeEffort");

	}

	private void bind(Viewer viewer, String property) {
		IObservableValue releaseObserveWidget = ViewersObservables.observeSingleSelection(viewer);
		IObservableValue releaseObserveValue = PojoObservables.observeValue(dto, property);
		bindingContext.bindValue(releaseObserveWidget, releaseObserveValue, null, null);
	}

	private void bind(Control control, String property) {
		IObservableValue actualEffortObserveWidget = SWTObservables.observeText(control, SWT.Modify);
		IObservableValue actualEffortObserveValue = PojoObservables.observeValue(dto, property);
		bindingContext.bindValue(actualEffortObserveWidget, actualEffortObserveValue, null, null);
	}

}
